<template>
  <section class="main">
    <!-- 
      v-model="计算属性"
      默认是不可以的，但是给计算属性加set方法就可以了
     -->
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="selectAll"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <!-- 当任务已完成，可以给 li 加上 completed 类，会让元素加上删除线 -->
      <li
        :class="{ completed: item.isDone }"
        v-for="item in newList"
        :key="item.id"
      >
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            :checked="item.isDone"
            @change="changeCheckbox(item.id)"
          />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="delClick(item.id)"></button>
        </div>
      </li>
    </ul>
  </section>
</template>
<script>
export default {
  props: ['list', 'type'],
  computed: {
    newList () {
      if (this.type === 'active') {
        // 进行中
        return this.list.filter(item => item.isDone === false)
      } else if (this.type === 'completed') {
        // 完成
        return this.list.filter(item => item.isDone === true)
      }
      // 全部
      return this.list
    },
    selectAll: {
      get () {
        return this.list.every(item => item.isDone === true)
      },
      set (value) {
        this.list.forEach(item => {
          item.isDone = value
        })
        // console.log(value)
      }
    }
  },
  methods: {
    // 删除功能
    delClick (id) {
      this.$emit('del', id)
    },
    // 修改状态
    changeCheckbox (id) {
      // console.log('changeCheckbox')
      this.$emit('setState', id)
    }
  }
}
</script>
<style></style>
